#include <QByteArray>
#include <QXmlAttributes>
#include <QXmlStreamReader>
#include <QString>
#include <QDebug>
#include <QDateTime>

#include "_Interface/_global_datastruct.h"
#include "backinstance.h"
#include "Package/oxfordlasersensordll_global.h"
#include "Package/simpletcpsocketdll_global.h"
#include "Package/SimpleRoboticMath/ConsoleMain/simpleroboticmath.h"

void backinstance::LaserSensorDataSave(){
    bool AllowDataSave = this->pLaserSensorData->bRecord;

    if(AllowDataSave == true){
        if((this->mDataSaveParam.SaveFlagLast == false)){
            static QString FileSaveMessage;

            QDateTime current_date_time = QDateTime::currentDateTime();
            QString   current_time = current_date_time.toString("hh-mm-ss");
            QString   current_date = current_date_time.toString("yyyy-MM-dd-");
            QString   filename_data = "../" + current_date + current_time + "-laser-data.csv";

            this->mDataSaveParam.mSaveFile = new QFile(filename_data);
            this->mDataSaveParam.mSaveFile->open(QIODevice::WriteOnly | QIODevice::Text);
            this->mDataSaveParam.mStreamData = new QTextStream(mDataSaveParam.mSaveFile);

            *(mDataSaveParam.mStreamData) << "Time" << ","
                                          << "Used"    << ","
                                          << "X"     << ","
                                          << "Y"     << ","
                                          << "Z"     << ","
                                          << "A"     << ","
                                          << "B"     << ","
                                          << "C"     << "\n";

            mDataSaveParam.TimeOrderLast = QDateTime::currentDateTime().toMSecsSinceEpoch();
            FileSaveMessage = "&nbsp;&nbsp;&nbsp;&nbsp;" + filename_data;
            mDataSaveParam.SaveFlagLast = true;
        }
        else if((this->mDataSaveParam.SaveFlagLast == true)){
            int    Value  = 0;
            if(this->ReadLaserSensorDataValue()){Value = 1;}
            mDataSaveParam.TimeOrder = QDateTime::currentDateTime().toMSecsSinceEpoch();
            mDataSaveParam.TimeOrderNow = mDataSaveParam.TimeOrder - mDataSaveParam.TimeOrderLast;
            if(mDataSaveParam.TimeOrderNow >= 25){
                *(mDataSaveParam.mStreamData) << mDataSaveParam.TimeOrderNow << ","
                                              <<Value    << ","
                                              <<this->vecLaserSeamDataByPCS.end()->POSXYZ[0] << ","
                                              <<this->vecLaserSeamDataByPCS.end()->POSXYZ[1] << ","
                                              <<this->vecLaserSeamDataByPCS.end()->POSXYZ[2] << ","
                                              <<this->vecLaserSeamDataByPCS.end()->EULERZYX[0] << ","
                                              <<this->vecLaserSeamDataByPCS.end()->EULERZYX[0] << ","
                                              <<this->vecLaserSeamDataByPCS.end()->EULERZYX[0] << "\n";
                this->mDataSaveParam.TimeOrderLast = mDataSaveParam.TimeOrder;
            }
        }
    }
    else{
        if((this->mDataSaveParam.SaveFlagLast == true)){
            mDataSaveParam.mSaveFile->close();
            delete mDataSaveParam.mSaveFile;
            delete mDataSaveParam.mStreamData;
            this->mDataSaveParam.SaveFlagLast = false;
        }
        else{

        }
    }
}
